package com.motion.controller;



import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.motion.dto.ArticleListDto;
import com.motion.dto.healthDto;
import com.motion.generator.entity.Article;
import com.motion.generator.entity.UserArticle;
import com.motion.result.Result;
import com.motion.result.ResultFactory;
import com.motion.service.ArticleService;
import com.motion.service.UserArticleService;
import com.motion.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/DietAndHealth")
@Api("健康饮食模块模块")
public class DietAndHealthControllor {
    @Autowired
    public ArticleService articleService;
    @Autowired
    public UserService userService;
    @Autowired
    public UserArticleService userArticleService;
    @RequestMapping(value = "/dietArticles", method = RequestMethod.GET)
    public Result dietArticles(@RequestParam Long page, @RequestParam Long size, @RequestParam String token){
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        IPage<Article> articleIPage= articleService.articleIPage(page,size,2);
        ArticleListDto ref=new ArticleListDto();
        ref.setDietArticleList(articleService.Arcle2UserArticlesDto(articleIPage.getRecords(),userId));
        ref.setHaveNext(articleIPage.getPages()*articleIPage.getSize()<articleIPage.getTotal());
        ref.setTotalPages(articleIPage.getTotal());
        return ResultFactory.buildSuccessResult(ref);
    }
    @RequestMapping(value = "/healthArticles", method = RequestMethod.GET)
    public Result healthArticles(@RequestParam Long page,@RequestParam Long size,@RequestParam String token){
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        IPage<Article> articleIPage= articleService.articleIPage(page,size,1);
        healthDto ref=new healthDto();
        ref.setHealthList(articleService.Arcle2UserArticlesDto(articleIPage.getRecords(),userId));
        ref.setHaveNext(articleIPage.getPages()*articleIPage.getSize()<articleIPage.getTotal());
        ref.setTotalPages(articleIPage.getTotal());
        return ResultFactory.buildSuccessResult(ref);
    }
    @RequestMapping(value = "/collectArticle", method = RequestMethod.GET)
    public Result collectArticle(@RequestParam Long articleId,@RequestParam String token){
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        UserArticle userArticle=new UserArticle();
        userArticle.setArticleId(articleId);
        userArticle.setUserId(userId);
        userArticle.setCreateTime(LocalDateTime.now());
        return ResultFactory.buildSuccessResult(userArticleService.insert(userArticle));
    }
    @RequestMapping(value = "/cancelCollectArticle", method = RequestMethod.GET)
    public Result cancelCollectArticle(@RequestParam String token,@RequestParam Long articleId){
        if(StpUtil.getLoginIdByToken(token)==null){
            return ResultFactory.buildResultTokenError("token无效");
        }
        Long userId = Long.valueOf(String.valueOf(StpUtil.getLoginIdByToken(token)));
        if(userArticleService.userifCollect(userId,articleId)) {
            return ResultFactory.buildSuccessResult(userArticleService.delect(articleId));
        }
        return ResultFactory.buildFailResult("你不是该用户");
    }
}
